﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microhard.WarehouseInsp.Buz.Interfaces;
using Microhard.Core.DBUtility;
using Microhard.WarehouseInsp.Buz.DataAccess;
using Microhard.WarehouseInsp.Buz.Models;
using Microhard.WarehouseInsp.Buz.DTO;
using Microhard.WarehouseInsp.Buz.BuzEnum;
using Microhard.Permission.Models;

namespace Microhard.WarehouseInsp.Buz.Impls
{
    public class SuperviseEnterpriseReportImpl : ISuperviseEnterpriseReport
    {


        public ExecutedResult Add(SuperviseEnterpriseReportModel model)
        {
            throw new NotImplementedException();
        }

        public ExecutedResult Update(SuperviseEnterpriseReportModel model)
        {
            throw new NotImplementedException();
        }

        public ExecutedResult Delete(List<Guid> ids)
        {
            throw new NotImplementedException();
        }

        public ExecutedResult Delete(SuperviseEnterpriseReportModel model)
        {
            throw new NotImplementedException();
        }

        public SuperviseEnterpriseReportModel GetModelByID(Guid id)
        {
            throw new NotImplementedException();
        }

        public SuperviseEnterpriseReportModel GetModelByModel(SuperviseEnterpriseReportModel model)
        {
            throw new NotImplementedException();
        }

        public List<SuperviseEnterpriseReportModel> Query(SuperviseEnterpriseReportModel Model, int pageIndex, int pageSize, out int totalCount)
        {
            throw new NotImplementedException();
        }

        public List<SuperviseEnterpriseReportModel> Query(SuperviseEnterpriseReportModel Model)
        {
            List<SuperviseEnterpriseReportModel> result = new List<SuperviseEnterpriseReportModel>();
            WarehouseInspEntities db = new WarehouseInspEntities();
            var data = (from p in db.SupProjects
                        from l in p.PledgePlanInRecords
                        where p.CreateTime >= Model.StartTime && p.CreateTime <= Model.EndTime
                        select l).ToList();
            data.ForEach(l =>
            {
                var newadd = new SuperviseEnterpriseReportModel()
                    {
                        Project = l.SupProject.ToModel(),
                        PlanPlg = l.ToModel()
                    };
                //获得监管底线
                var gl = l.SupProject.SupervisionDetails.Where(s => s.Status == (int)PrjStatus.Finished).OrderByDescending(c => c.CreateTime);
                if (gl.Count() > 0)
                {
                    newadd.GuideLine = gl.First().Guardline.Value;
                }
                //获得最新库存合计
                var cur = l.PledgeChangeRecords.Where(i => i.IsUsing == true);
                if (cur.Count() > 0)
                {
                    newadd.CurrentAmount = cur.First().CurrentAmount.Value;
                }
                //贷款期限
                newadd.RentRange = l.SupProject.PrjStartTime.Value.ToString("yyyy-MM-dd") + "至" +
                     l.SupProject.PrjEndTime.Value.ToString("yyyy-MM-dd");
                //状态
                DictionaryCache dc = new DictionaryCache();
                var prjtypeid = l.SupProject.PrjType.Value;
                var dic = dc.GetFromCache(prjtypeid.ToString());
                newadd.PrjType = dic.Name;

                result.Add(newadd);
            });
            return result;
        }



        public Microhard.Core.DBUtility.ScopeResult scope
        {
            set;
            get;
        }
    }
}
